﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.IO;
using Com.CodePlex.SPPower.Data;
using Com.CodePlex.SPPower.Controller;
using Com.CodePlex.SPPower.Model;
using System.Windows.Input;
using System.Text;
using System.Text.RegularExpressions;
//using Com.CodePlex.SPPower.View; //should not reference this http://social.msdn.microsoft.com/Forums/is/wpf/thread/3bced3ea-4a97-4456-bfe5-828289bcf971

namespace Com.CodePlex.SPPower.ViewModel {

	public class HistoryViewModel : ViewModel {

		public string Cmd { get; private set; }
		public DateTime Timestamp { get; private set; }

		public HistoryViewModel(string cmd, DateTime timestamp) {
			Cmd = cmd;
			Timestamp = timestamp;
            UpdateDisplayName(); 
			Mediator.Register(this);
			//note: lazy load: dont do more stuff here
		}

        #region Commands

        private ICommand loadcommand;
        public ICommand LoadCommand { get { return loadcommand ?? (loadcommand = new RelayCommand(param => Load())); } }

        private ICommand deletecommand;
        public ICommand DeleteCommand { get { return deletecommand ?? (deletecommand = new RelayCommand(param => Delete())); } }

        #endregion

		#region Display Properties

		private bool isexpanded;
		public bool IsExpanded {
			get { return isexpanded; }
			set {
				if (isexpanded != value) {
					isexpanded = value;
					OnPropertyChanged(() => IsExpanded);
				}
			}
		}

		private bool isautoscroll = false;
		public bool IsAutoScroll {
			get { return isautoscroll; }
			set {
				if (isautoscroll != value) {
					isautoscroll = value;
					OnPropertyChanged(() => IsAutoScroll);
				}
			}
		}

		private string log;
		public string Log {
			get { return log; }
			set {
				if (value != log) {
					log = value;
					OnPropertyChanged(() => Log);
				}
			}
		}

        private IList<Service.CmdParameter> parameters;
        public IList<Service.CmdParameter> Parameters {
            get { return parameters; }
            set {
                parameters = value;
                UpdateDisplayName();
            }
        }
		#endregion

        public void UpdateDisplayName() {
            string result = Timestamp + ": " + Cmd;
            if (parameters != null) {
                result += " (";
                int count = 0;
                parameters.ToList().ForEach(i => {
                    if (i.Value != null) {
                        if (count++ > 0) {
                            result += ", ";
                        }
                        result += i.Name + "=" + i.Value;
                    }
                });
                result += ")";
            }
            DisplayName = result;
        }

        public void Load() {
            Mediator.Notify<SimpleMessage>(SimpleMessage.LoadHistoryItem, new SimpleMessage() { Object = this });
        }

        public void Delete() {
            
        }
    }
}